﻿Imports DevComponents.DotNetBar
Imports quanlydulich.Helpers

Partial Public Class TOUR
    Inherits Office2007Form
    Private objDS As DataSet
    ' Event handler is used to transfer data to receiver
    Public Event Handler(ByVal sender As Object, ByVal e As MyEvent)

    Public Criterial_ID As String
    Public Criterial_Name As String
    Public Criterial_FromPrice As String
    Public Criterial_ToPrice As String
    Public Criterial_StartDate As String
    Public Criterial_EndDate As String
    Public ModeSearch As Boolean

    Private Sub TOUR_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()
    End Sub

    ' Load list of TOUR from DB
    Public Sub LoadData()
        Dim objBUS As TOURBUS = New TOURBUS(GlobalVars.g_strConnectionString)

        If Not ModeSearch Then
            objDS = objBUS.SelectAll()
        Else
            objDS = objBUS.Search(Criterial_ID, Criterial_Name, Criterial_FromPrice, Criterial_ToPrice, DateTime.Parse(Criterial_StartDate), DateTime.Parse(Criterial_EndDate))
        End If

        ' Neu ton tai du lieu ung voi chuc vu table trong DB
        dtgvDataList.AutoGenerateColumns = False
        dtgvDataList.DataSource = objDS.Tables(0)
        dtgvDataList.ClearSelection()
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        If txtTenTOUR.Text = "" Then
            MessageBox.Show("Vui lòng nhập tên tour", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        ElseIf txtDonGia.Text = "" Then
            MessageBox.Show("Vui lòng nhập đơn giá", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        ' Neu user chua select in grid view -> thuc hien thao tac insert
        If txtMaTOUR.Text = "" Then
            InsertTOUR()
        Else ' Thuc hien thao tac update
            UpdateTOUR()
        End If

    End Sub

    Public Sub InsertTOUR()
        Dim objBUS As TOURBUS = New TOURBUS(GlobalVars.g_strConnectionString)
        Dim objDTO As TOURDTO = New TOURDTO
        objDTO.TenTour = txtTenTOUR.Text
        objDTO.DonGia = Long.Parse(txtDonGia.Text.Replace(",", ""))
        objDTO.GioiThieu = txtGioiThieu.Text
        objDTO.NgayBD = dtNgayBD.Value
        objDTO.NgayKT = dtNgayKT.Value

        If objBUS.Insert(objDTO) Then
            LoadData()
            EraseTextboxes()
            MessageBox.Show("An item has been Added")
        Else
            MessageBox.Show("Add failed")
        End If
    End Sub

    Public Sub UpdateTOUR()
        Dim objBUS As TOURBUS = New TOURBUS(GlobalVars.g_strConnectionString)
        Dim objDto As TOURDTO = New TOURDTO
        objDto.ID = Integer.Parse(txtMaTOUR.Text)
        objDto.TenTour = txtTenTOUR.Text
        objDto.DonGia = Long.Parse(txtDonGia.Text.Replace(",", ""))
        objDto.GioiThieu = txtGioiThieu.Text
        objDto.NgayBD = dtNgayBD.Value
        objDto.NgayKT = dtNgayKT.Value

        If objBUS.Update(objDto) Then
            LoadData()
            MessageBox.Show("A new item has been updated")
        Else
            MessageBox.Show("Update failed!")
        End If
    End Sub

    ' Lay thong tin cua chuc vu khi nguoi dung select in grid view
    Private Sub dtgvTOUR_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgvDataList.CellClick
        Dim selectedRow As Integer = dtgvDataList.CurrentRow.Index
        If selectedRow <> -1 Then
            txtMaTOUR.Text = dtgvDataList.Item(0, selectedRow).Value
            Dim objBus As TOURBUS = New TOURBUS(GlobalVars.g_strConnectionString)
            Dim lstItem As DataTable = objBus.SelectByID(Integer.Parse(txtMaTOUR.Text)).Tables(0)
            If lstItem.Rows.Count > 0 Then
                txtTenTOUR.Text = lstItem.Rows(0)("TenTour").ToString()
                txtDonGia.Text = lstItem.Rows(0)("DonGia").ToString()
                txtGioiThieu.Text = lstItem.Rows(0)("GioiThieu").ToString()
                dtNgayBD.Value = DateTime.Parse(lstItem.Rows(0)("NgayBD").ToString())
                dtNgayKT.Value = DateTime.Parse(lstItem.Rows(0)("NgayKT").ToString())
            End If
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim objBUS As TOURBUS = New TOURBUS(GlobalVars.g_strConnectionString)
        ' Chi thuc hien xoa, khi user select in grid view
        If txtMaTOUR.Text <> "" Then
            If objBUS.Delete(Integer.Parse(txtMaTOUR.Text)) Then
                LoadData()
                EraseTextboxes()
                MessageBox.Show("A new item has been deleted")
            Else
                MessageBox.Show("Delete failed!")
            End If
        End If

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnNhapMoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapMoi.Click
        EraseTextboxes()
    End Sub

    Public Sub EraseTextboxes()
        txtMaTOUR.Text = ""
        txtTenTOUR.Text = ""
        txtDonGia.Text = ""
        txtGioiThieu.Text = ""
        dtgvDataList.ClearSelection()
    End Sub

    Private Sub btnChon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChon.Click
        Close()
        RaiseEvent Handler(Me, New MyEvent(txtMaTOUR.Text & "#" & txtTenTOUR.Text))
    End Sub

    ' This variable is used to end changing on textbox
    Private endChange As Boolean = False
    Private Sub txtDonGia_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDonGia.TextChanged
        If endChange Then
            endChange = False
            Me.txtDonGia.SelectionStart = Me.txtDonGia.Text.Length
            Return
        End If
        If txtDonGia.Text.Length > 0 Then
            Dim strOrigin = txtDonGia.Text.Replace(",", "")
            Dim strFormated = String.Format("{0:#,##0;Nothing}", Long.Parse(strOrigin))
            If strOrigin <> strFormated Then
                endChange = True
                Me.txtDonGia.Text = strFormated
            End If
        End If
    End Sub

    Private Sub txtDonGia_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDonGia.KeyPress
        ' Only allow type number
        If Not Char.IsNumber(e.KeyChar) And e.KeyChar <> Chr(Keys.Back) Then
            e.Handled = True
        End If
    End Sub

    Private Sub btnXemPhieuDatTour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXemPhieuDatTour.Click
        Dim selectedRow As Integer = dtgvDataList.CurrentRow.Index
        If selectedRow <> -1 Then
            Dim objBus As TOURBUS = New TOURBUS(GlobalVars.g_strConnectionString)
            Dim lstItem As DataTable = objBus.SelectByID(Integer.Parse(dtgvDataList.Item(0, selectedRow).Value)).Tables(0)

            Dim frm As LAPPHIEUDATTOUR = New LAPPHIEUDATTOUR
            frm.MaTour = dtgvDataList.Item(0, selectedRow).Value
            frm.TenTour = lstItem.Rows(0)("TenTour").ToString()
            frm.ShowDialog()
        End If    
    End Sub

    Private Sub dtgvDataList_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgvDataList.DoubleClick
        Dim selectedRow As Integer = dtgvDataList.CurrentRow.Index
        If selectedRow <> -1 Then
            Dim objBus As TOURBUS = New TOURBUS(GlobalVars.g_strConnectionString)
            Dim lstItem As DataTable = objBus.SelectByID(Integer.Parse(dtgvDataList.Item(0, selectedRow).Value)).Tables(0)

            Dim frm As LAPPHIEUDATTOUR = New LAPPHIEUDATTOUR
            frm.MaTour = dtgvDataList.Item(0, selectedRow).Value
            frm.TenTour = lstItem.Rows(0)("TenTour").ToString()
            frm.ShowDialog()
        End If
    End Sub
End Class